package com.mashibing.juc.c_026_00_Interview.A1B2C3;

/**
 * @author: Yuanzt
 * @Date: 2021/4/2
 */
public class T06_00_sync_wait_notify {

    public static void main(String[] args) {
        final Object o = new Object();

        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        new Thread(()->{
            synchronized (o){
                for (char c : aI) {
                    System.out.print(c);
                    try {
                        o.notify();//叫醒其他线程
                        o.wait();//让出锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();//停止程序
            }
        },"t1").start();

        new Thread(()->{
            synchronized (o){
                for (char c : aC) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        },"t2").start();
    }
}
